package b_nio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;

public class NIOClient {
    public static void main(String[] args) throws Exception {
        // 1.打开通道
        SocketChannel socketChannel = SocketChannel.open();
        // 2.设置连接IP和端口号
        socketChannel.connect(new InetSocketAddress("127.0.0.1", 9999));
        // 3.写出数据
        socketChannel.write(ByteBuffer.wrap("这是一条消息".getBytes(StandardCharsets.UTF_8)));
        // 4.读取服务器写回的数据
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        int len = socketChannel.read(byteBuffer);
        System.out.println("服务端：" + new String(byteBuffer.array(),
                0, len, StandardCharsets.UTF_8));
        // 5.释放资源
        socketChannel.close();
    }
}
